package appClientModule;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ActiveApplet {
	private static byte[] convertByHighToLow(byte[] retmsg, int length) {
		byte[] data = new byte[length];
		for (int i = 0; i < data.length; i++) {
			byte a = retmsg[i];
			int b1 = a << 4 & 0xf0;
			int b2 = a >> 4 & 0x0f;
			data[i] = (byte) (b1 + b2);
		}
		System.out.println(MsgGenUtil.buf2hex(data));
		return data;
	}

	public static void main(String[] args) {

		// ��λ

		// ѡ��2f00

		// ���ݼ�¼ѡ��aid

		String str = "{\"GetMTSMSResponse\":{\"ResultCode\":0,\"ResultDesc\":\"��ȫ��Կ�·�����ȡ�óɹ���\",\"MTSMSContent\":[\"0700037D030170000138150630151511000722EA49616AFF0CC79188B46C49EE635418920013DFB3E0BD73A1969CC290E8CFCD0DFD3C8FA016F016518B2C531ACF366A2EB0B5B0F4B26E64D54934D84A3D54471824C1BD5B306A74D15A1C2AAA3804FBE813125663CA7DE629D3929B65518736894B03511BBF2307ED88491CBB5EBED0703AFC6598F86852B1\",\"0500037D0302DF8A96A4FB10B1151B60F39CBF1721985C215161EA207D6CE123156ACE4A1756875DDF4CFA294C0CC00DFBDAB90968C35C226886F77094891218581E1E6B0FCB5148918B40D1115C5024053CC038611B666306FB13B1765A2CD488587B61B3C0C37610450819104D6D8AA4A322437FBFD54A77BADD96CAE7B24D6D934868F7E79B907822DD30\",\"0500037D030327647060375773A53CC19DE8E5894070956BCAC8EA03113EC2A4B8BFBC53EA095AF99632B0989FA5D09DC2041D20646A\"],\"ServerTime\":\"20170927153120\"}}";
		ObjectMapper mapper = new ObjectMapper();
		DownloadMsg gt = null;
		try {
			gt = mapper.readValue(str, DownloadMsg.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String[] coms = gt.GetMTSMSResponse.getMTSMSContent();
		for (String com : coms) {
			System.out.println(com);
		}

		// 0400367B200AC31D706798683051481000313369005702A924E55C8067014906116067976012500607588704EAE383AA
		String msg = "D05381030113008202818348480000021002040602041966190008390003200000013201802001B3D9005618EB833CC341828A408001899B4802B81549272AE403380A48308B033CBB009280303AC43827571C1D509000";
		// msg =
		// "D0538103011300820281834848000002100204060204196619000839000320000001320180200283F2E4F099469274C341828A408001899B4802B81549272AE403380A48308B033CBB009280308177F0DDE15642509000";
		// msg = msg.substring(4,msg.length()-4);
		// msg = activeAppletCap(msg);
		// System.out.println(msg);
		// 01B028001C48181B3000F2146D3B0F26985C4227F756B6365ADA8C705188745CCEDA3B7C5424836FCD062E620F8C458E912C98596A2D9510
		// 01B028001804481818033000F0021004680538030807200698580440022007F0075006B00630065802D80288047050018870045804C806D802380378045004200480036807C80500
		msg = "05000389030366001E428DA761E4D30B8844FEEAD6C6CB5B518E0A310E8B99DB476F8A84906DF9A0C5CC41F188B1D225930B2D45B2A2";
		msg = "0700038903017000013815063015151100075C8A531DE7391056FE638B4C59ED902486C8EF25189A5D1363FBBDAE1A3E28DCAFD295D37F4994822CB5287289893ACEA80CCC382EFF32A9ACA3434C7E42D63F18004BDFFDF889C7D7372FD59B9C748AFF59331092E75AC50BCC3F9FD16AF4EF7E89D036D28E5E653631AA197838A4873C1F96B048B8421FECB7";
		msg = downMsg(msg);
		System.out.println("result:" + msg);
	}

	public static String downMsg(String msg) {
		String index = msg.substring(10, 12);
		String aa = "";
		switch (index) {
		case "01":
			index = "060138";
			aa = "10A968";
			break;
		case "02":
			index = "060144";
			aa = "110808";
			break;
		case "03":
			index = "0601F8";
			aa = "10CB68";
			break;
		}
		String length = MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(msg.length() / 2));
		msg = length + msg;
		byte[] data = MsgGenUtil.BCD2Bytes(msg);
		byte[] ret = new byte[data.length + 1];
		byte sss[] = new byte[1];
		for (int i = data.length - 1; i >= 0; i--) {
			System.out.println(i);
			if (i == data.length - 1) {
				sss[0] = data[i];
				int f = MsgGenUtil.makeInt(sss);
				int n = f << 3;
				ret[i + 1] = MsgGenUtil.Int2Bytes1(n)[0];
				System.out.println(MsgGenUtil.buf2hex(ret));
			} else {
				String curr = (Long.toString(data[i] & 0xff, 2));
				curr = buw(curr);
				String curr5 = curr.substring(3);// ��ǰ�ֽڵ�5λ
				String next = (Long.toString(data[i + 1] & 0xff, 2));
				next = buw(next);
				String next3 = next.substring(0, 3);// �¸��ֽڵĸ���3λ
				ret[i + 1] = conver2HexToByte(curr5 + next3)[0];
			}
			if (i == 0) {
				String curr5 = "00000";// ��ǰ�ֽڵ�5λ
				String next = (Long.toString(data[i] & 0xff, 2));
				next = buw(next);
				String next3 = next.substring(0, 3);// �¸��ֽڵĸ���3λ
				ret[i] = conver2HexToByte(curr5 + next3)[0];
			}
		}
		System.out.println(MsgGenUtil.buf2hex(ret));
		String resp = MsgGenUtil.buf2hex(ret);
		resp = "0003" + aa + "01" + MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(resp.length() / 2)) + resp
				+ "03061708171444360801400A01400F01E0";
		resp = "00000210070206020699661900" + index + "08"
				+ MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(resp.length() / 2)) + resp;
		String leng = getLeng(resp);
		resp = "0202838148" + leng + resp;
		leng = getLeng(resp);
		resp = "D1" + leng + resp;
		resp = "80C20000" + MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(resp.length() / 2)) + resp;
		System.out.println(resp);
		return resp;
	}

	private static String getLeng(String resp) {
		resp = MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(resp.length() / 2));
		int len = Integer.parseInt(resp, 16);
		if (len > 128) {
			resp = "81" + resp;
		}
		return resp;
	}

	public static String buw(String data) {
		for (int t = data.length(); data.length() < 8; t++) {
			data = "0" + data;
		}
		return data;
	}

	public static String activeAppletCap(String msg) {
		boolean flag = true;
		Tlv tlv = new Tlv();
		tlv = tlv.parser(msg);
		if (tlv.isNoShorMsg()) {
			return "";
		}
		byte[] data = MsgGenUtil.BCD2Bytes(tlv.getV());
		byte[] ret = null;
		int len = 0;

		while (flag) {
			String a = (Long.toString(data[len] & 0xff, 2));
			if (a.length() > 3) {
				a = a.substring(a.length() - 3);
			}
			String b = (Long.toString(data[len + 1] & 0xff, 2));
			if (b.length() <= 3) {
				b = "00000";
			} else {
				for (int i = b.length(); i < 8; i++) {
					b = "0" + b;
				}
				b = b.substring(0, 5);
			}
			if (len == 0) {
				int dataLength = Integer.parseInt(MsgGenUtil.buf2hex(conver2HexToByte(a + b)), 16);
				ret = new byte[dataLength];
			} else
				ret[len - 1] = conver2HexToByte(a + b)[0];
			len++;
			if (len == data.length - 1)
				flag = false;
		}
		return MsgGenUtil.buf2hex(ret);

	}

	/**
	 * �������ַ���ת��Ϊbyte����,ÿ���ֽ���","����
	 **/
	public static byte[] conver2HexToByte(String hex2Str) {
		String[] temp = hex2Str.split(",");
		byte[] b = new byte[temp.length];
		for (int i = 0; i < b.length; i++) {
			b[i] = Long.valueOf(temp[i], 2).byteValue();
		}
		return b;
	}

	public List<Apdu> initCommand() {
		List<Apdu> list = new ArrayList<Apdu>();
		Apdu apdu = new Apdu();
		apdu.setIndex("1");
		apdu.setAPDU("801000001EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000");
		String[] sw = new String[] { "91" };
		apdu.setSW(sw);
		Apdu apdu2 = new Apdu();
		apdu2.setIndex("1");
		apdu2.setAPDU("801200000F");
		String[] sw2 = new String[] { "9000" };
		apdu2.setSW(sw2);
		list.add(apdu);
		list.add(apdu2);
		return list;
	}
	//�����õĵ�������
	public static void maa() {
		/*ActiveApplet aa = new ActiveApplet();
		String downmsg = aa.downMsg(param);
		ret = sendApdu(downmsg);
		Map upMap = new HashMap();
		if (!ret.substring(ret.length() - 4).equals("9000")) {
			upMap.put("resultCode", 1);
		} else {
			upMap.put("resultCode", 0);
		}
		upMap.put("resultIndex", downmsg);
		upMap.put("resultResponse", ret.substring(0, ret.length() - 4));
		upMap.put("resultSW", ret.substring(ret.length() - 4));
		resp = mapper.writeValueAsString(upMap);
		applet_pcsc.this.ApduRsp = resp;
		return applet_pcsc.this.ApduRsp;*/
	}
}
